package com.yf.course.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.course.modules.course.dto.request.CourseSpeechReqDTO;
import com.yf.course.modules.course.dto.request.CourseVideoReqDTO;
import com.yf.course.modules.course.entity.CourseSpeech;
import com.yf.course.modules.course.entity.Speech;
import com.yf.course.modules.course.entity.Video;
import com.yf.course.modules.course.mapper.SpeechMapper;
import com.yf.course.modules.course.service.SpeechService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程讲稿业务实现类
 * </p>
 */
@Service
public class SpeechServiceImpl extends ServiceImpl<SpeechMapper, Speech> implements SpeechService {
    /**
     * 新增或修改课程讲稿
     * @param courseSpeechReqDTO 相关参数
     */
    public Boolean save(CourseSpeechReqDTO courseSpeechReqDTO) {
        Speech speech = new Speech(courseSpeechReqDTO);
        speech.insertOrUpdate();
        // 新增讲稿的情况
        if (courseSpeechReqDTO.getCourseId()!= null) {
            CourseSpeech courseSpeech = new CourseSpeech();
            courseSpeech.setCourseId(courseSpeechReqDTO.getCourseId());
            courseSpeech.setSpeechId(speech.getId());
            courseSpeech.insert();
        }
        return true;
    }

    /**
     * 删除讲稿
     * @param speechId 讲稿ID
     */
    public Boolean delete(String speechId) {
        Speech speech = new Speech();
        speech.setId(speechId);
        // el_speech 删除记录
        speech.deleteById();
        // el_course_speech 删除记录
        QueryWrapper<CourseSpeech> courseSpeechQueryWrapper = new QueryWrapper<>();
        courseSpeechQueryWrapper.lambda().eq(CourseSpeech::getSpeechId, speechId);
        new CourseSpeech().delete(courseSpeechQueryWrapper);
        return true;
    }

    /**
     * 分页查询数据
     * @param reqDTO 分页参数
     */
    public IPage<Speech> paging(PagingReqDTO<CourseSpeechReqDTO> reqDTO) {
        Page<Speech> page = reqDTO.toPage();
        CourseSpeechReqDTO params = reqDTO.getParams();

        QueryWrapper<Speech> speechWrapper = new QueryWrapper<>();
        if (params != null && StringUtils.isNotBlank(params.getCourseId())) {
            speechWrapper.lambda().inSql(
                    Speech::getId,
                    "SELECT speech_id FROM el_course_speech WHERE course_id = '"
                            + params.getCourseId() + "'"
            );
        }

        IPage<Speech> result = this.page(page, speechWrapper);
        // 回填 courseId 字段，方便前端展示
        if (params != null && StringUtils.isNotBlank(params.getCourseId())) {
            result.getRecords()
                    .forEach(s -> s.setCourseId(params.getCourseId()));
        }

        return result;
    }
}
